//
//  NetworkTools.m
//  oc封装AFN
//
//  Created by 王、小胜 on 16/7/22.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "NetworkTools.h"

@implementation NetworkTools


+(instancetype)sharedTools{
  
    static NetworkTools *tools;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //注意末尾包含“/”
        NSURL *baseUrl=[NSURL URLWithString:@"http://www.weather.com.cn/"];
        tools = [[self alloc] initWithBaseURL:baseUrl];
        
        //设置反序列化
        tools.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application.json",@"text/json",@"text/javascript",@"text/html", nil];
        
    });
    return tools;
}
//Block形式函数回调
-(void)request:(NSString *)urlString parameters:(id)parameters finished:(void (^)(id result,NSError *error))finished{
    
    [self GET:urlString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        //NSLog(@"%@",responseObject);
        finished(responseObject,nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //NSLog(@"%@",error);
        finished(nil,error);
    }];
    
}


////简单的Afn封装
//-(void)request:(NSString *)urlString parameters:(id)parameters{
//    [self GET:urlString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
//        NSLog(@"%@",responseObject);
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"%@",error);
//    }];
//}
@end
